﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using SmlEngine.Graphics;

namespace SmlEngine.Extensions
{
    public static class Texture2DExtensions
    {
        /// <summary>
        /// Loads a texture from a file.
        /// </summary>
        /// <param name="path">The file path to the image to use.</param>
        /// <param name="gd">The GraphicsDevice to use.</param>
        /// <returns>The loaded texture.</returns>
        public static Texture2D LoadFromFile(this Texture2D texture, string path, ref GraphicsDevice gd)
        {
            using (Stream stream = File.OpenRead(path))
            {
                return Texture2D.FromStream(gd, stream);
            }
        }

        /// <summary>
        /// Loads and crops a texture from a file.
        /// </summary>
        /// <param name="path">The file path to the image to use.</param>
        /// <param name="gd">The GraphicsDevice to use.</param>
        /// <param name="size">The final size of the texture.</param>
        /// <returns>The loaded, cropped texture.</returns>
        public static Texture2D LoadFromFile(this Texture2D texture, string path, ref GraphicsDevice gd, Rectangle size)
        {
            Texture2D t;
            using (Stream stream = File.OpenRead(path))
            {
                t = Texture2D.FromStream(gd, stream);
            }
            return t.Crop(size);
        }
    }
}
